package org.r.tools;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.Map;
import java.util.StringJoiner;

public class RStringUtils {

    /**
     *  拼接字符串
     * @param joinMark
     * @param joinObj
     * @return
     */
    public static String join(String joinMark, String ...joinObj){
        StringJoiner sj = new StringJoiner(joinMark);
        for (String str : joinObj) {
            if(StrUtil.isNotBlank(str)){
                sj.add(str);
            }
        }
        return sj.toString();
    }


    /**
     *
     * @param context
     * @param maxLength
     * @return
     */
    public static String substring(String context,int maxLength){
        return context.length() > maxLength ? context.substring(0, maxLength - 1) : context;
    }

    public static String substring(String context){
        return context.length() > 1024 ? context.substring(0, 1023) : context;
    }
    /**
     *  美化json字符串
     * @param map
     * @return
     *  原字符串： String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
     *  美化后：
     *  {
     *   "name" : "John",
     *   "age" : 30,
     *   "city" : "New York"
     * }
     */
    public static String beautifyFormatJson(Map map) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            return mapper.writeValueAsString(mapper.readValue(JSON.toJSONString(map), Object.class));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     *  美化json字符串
     * @param jsonString
     * @return
     */
    public static String beautifyFormatJson(String jsonString) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            return mapper.writeValueAsString(mapper.readValue(jsonString, Object.class));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     *  美化json字符串
     * @param obj
     * @return
     */
    public static <T>String beautifyFormatJson(T obj) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            return mapper.writeValueAsString(mapper.readValue(JSON.toJSONString(obj), Object.class));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     *  美化json字符串
     * @param jsonObject
     * @return
     */
    public static String beautifyFormatJson(JSONObject jsonObject) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            return mapper.writeValueAsString(mapper.readValue(jsonObject.toJSONString(), Object.class));
        } catch (Exception e) {
            return "";
        }
    }

}
